***********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"********
***********************************************************************************
* This file sets up some of datasets 
* that will be used for the 2019 analyses.
***********************************************************************************
***********************************************************************************


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/type2019, case(lower)

rename abi bankid
sort bankid
labmask type, values(description)
drop description

**# Bookmark (saving dataset - bank type - e.g. joint stock company, mutual bank)
compress
save $inputdirectory/INB_type2019, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/gru2019, case(lower)

rename ente_segn bankid
duplicates drop gru bankid, force

* Changes suggested by the supervisory department
* LINES NOT SHOWN

**# Bookmark (saving dataset - map of the groups)
compress
save $inputdirectory/INB_gru2019, replace


*==================================================================================
**# Boomark (input)
clear *
import sas using $inputdirectory/fs2019

rename ABI bankid
rename VOCE voce
rename IMPORTO importo
drop DATA
keep if inlist(voce, 52000600)

format importo %20.0g
* LINE NOT SHOWN (NAMES OF BANKS/BALANCE-SHEET ITEMS ALREADY DROPPED IN THE PSEUDO DATASET)
reshape wide importo, i(bankid) j(voce)
rename importo52000600 cash
recode * (.=0)

**# Bookmark (saving dataset - cash from the financial statement)
compress
save $inputdirectory/INB_fs2019, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/netinterbank2019, case(lower)

reshape wide valore, i(bankid) j(vocesotvoc)
recode valore* (.=0)
replace valore5843502=-valore5843502
replace valore5843506=-valore5843506
egen netinterbank=rowtotal(valore5840536 valore5840540 valore5840548 valore5843502 valore5843506)

keep bankid netinterbank

**# Bookmark (saving dataset - net interbank position as defined in Appendix II)
compress
save $inputdirectory/INB_netintbank2019, replace


*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/tltro2019, case(lower)

rename abi bankid
rename importo extltro
format extltro %20.0g

**# Bookmark (saving dataset - TLTRO III exposure as defined in Appendix II)
compress
save $inputdirectory/INB_tltro2019, replace

*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/capital2019, case(lower)

drop data_oss
rename ente_segn gru
replace cet1ratio=100*cet1ratio
replace t1ratio=100*t1ratio
replace totcap_ratio=100*totcap_ratio

**# Bookmark (saving dataset - consolidated capital ratios)
compress
save $inputdirectory/INB_capital2019, replace

*==================================================================================
**# Bookmark (input)
clear *
import sas using $inputdirectory/comprov

rename PROVINCIA prov
rename COMUNE comune_dip
rename REGIONE reg
rename AREGEO macro

preserve
keep if (ENDDATE>20190229)&(ENDDATE<20190831)
save xxx, replace
restore

preserve
keep if (STARTDATE>20190831&STARTDATE<=20200229)
save yyy, replace
restore

keep if (STARTDATE<=20190831&ENDDATE>20190831)
append using xxx
append using yyy

erase xxx.dta
erase yyy.dta

distinct comune_dip prov, join

duplicates tag comune_dip prov, g(dup)
sort comune_dip prov
duplicates drop comune_dip prov, force
distinct comune_dip prov, join

keep comune_dip prov reg macro

**# Bookmark (saving dataset - municipality to province mapping)
compress
save $inputdirectory/INB_comprov2019, replace
